<?php

namespace app\beautiful\controller\admin\setting;

use app\beautiful\model\mall\FreightTemplateModel;
use app\beautiful\model\setting\SendKuaidiModel;
use app\beautiful\model\setting\SendCityModel;
use app\beautiful\model\setting\SendSelfModel;
use app\BaseController;
use app\common\model\ShopModel;
use think\facade\Db;

class Send extends BaseController{

    public function setOpen(){
        $is_open = $this->request->param('is_open/d',0);
        $send_type = $this->request->param('send_type/d',0);
        $default_send_type = $this->defaultSendType();
        if($send_type<1) return $this->error('参数错误');

        if($send_type==1){
//            $model = new SendKuaidiModel();
            $model = new FreightTemplateModel();
        }
        if($send_type==2){
            $model = new SendSelfModel();
        }
        if($send_type==3){
            $model = new SendCityModel();
        }
        if($is_open<1 && $send_type==$default_send_type){
            return $this->error('默认配送方式不能关闭');
        }
        if(isset($model)){
            $model->where([['shop_id','=',$this->request->shopId]])->update(['is_open'=>$is_open]);
        }
        return $this->success('设置成功');
    }

    /**
     * 获取快递配送信息
     */
    public function showKuaidi()

    {
        if(null === $kuaidi_setting=SendKuaidiModel::where([['shop_id','=',$this->request->shopId]])->find()){

            $kuaidi_setting = new SendKuaidiModel();
            $kuaidi_setting->shop_id = $this->request->shopId;
            $kuaidi_setting->save();
        }

        $detail=[
            'is_open' => $kuaidi_setting->is_open ?? 0,
            'name' => $kuaidi_setting->name,
            'is_default' => $kuaidi_setting->is_default ?? 0,
            'is_follow' => $kuaidi_setting->is_follow ?? 0,
        ];

        return $this->success(['detail'=>$detail]);

    }

    /**
     * 保存快递配送信息
     */
    public function saveKuaidi()

    {

        $is_open = $this->request->param('is_open/d',0);
        $is_default = $this->request->param('is_default/d',0);
        $is_follow = $this->request->param('is_follow/d',0);
        $name = $this->request->param('name');

        if(!in_array($is_open,[0,1])) return $this->error('参数错误');
        if(!in_array($is_default,[0,1])) return $this->error('参数错误');
        if(!in_array($is_follow,[0,1])) return $this->error('参数错误');

        $default_send_type = $this->defaultSendType();

        Db::startTrans();

        try{
            if(null === $kuaidi_setting=SendKuaidiModel::where([['shop_id','=',$this->request->shopId]])->find()){

                $kuaidi_setting = new SendKuaidiModel();
                $kuaidi_setting->shop_id = $this->request->shopId;

            }

            $kuaidi_setting->is_open = $is_open;
            $kuaidi_setting->is_default = $is_default;
            $kuaidi_setting->is_follow = $is_follow;
            $kuaidi_setting->name = $name;
            $kuaidi_setting->save();

            if($is_default>0){
                if($is_open<1) return $this->error('默认配送方式不能关闭');

                if($default_send_type!==1) $this->updateDefaultSendType($default_send_type,['is_default'=>0]);

                ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>1]);
            }else{
                if($kuaidi_setting->is_default>0) return $this->error('必须开启一种默认配送方式');
//                if($default_send_type == 1){
//                    ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>0]);
//                }
            }

            Db::commit();

            return $this->success('保存成功');

        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
    }

    /**
     * 获取同城配送信息
     */
    public function showCity()

    {

        if(null === $city_setting=SendCityModel::where([['shop_id','=',$this->request->shopId]])->json(['send_scope','order_send_time','order_time_slot','ladder_freight_distance','reduce_freight_price','forward_order_send'],true)->find()){

            $city_setting = new SendCityModel();
            $city_setting->shop_id = $this->request->shopId;
            $city_setting->save();
        }
        $forward_order_send = $city_setting->forward_order_send;

        if(!$forward_order_send){

            $forward_order_send = [
                'forward_order_send'=>0
                ];
        }

        $detail=[
            'is_open' => $city_setting->is_open ?? 0,
            'name' => $city_setting->name,
            'is_default' => $city_setting->is_default ?? 0,
            'send_scope' => $city_setting->send_scope,
            'open_order_send' => $city_setting->open_order_send ?? 0,
            'open_order_day' => $city_setting->open_order_day ?? 0,
            'order_send_time' => $city_setting->order_send_time ??[],
            'order_time_slot' => $city_setting->order_time_slot ?? (object)[],
            'forward_order_send' => $forward_order_send ??[],
            'start_send_price' => $city_setting->start_send_price ?? '0.00',
            'base_freight' => $city_setting->base_freight ?? '0.00',
            'ladder_freight_distance' => $city_setting->ladder_freight_distance ??[],
            'reduce_freight_price' => $city_setting->reduce_freight_price ??[],
            'remarks' => $city_setting->remarks,

        ];

        return $this->success(['detail'=>$detail]);

    }

    /**
     * 保存同城配送信息
     */
    public function saveCity()

    {
        $is_open = $this->request->param('is_open/d',0);
        $is_default = $this->request->param('is_default/d',0);
        $open_order_send = $this->request->param('open_order_send/d',0);
        $open_order_day = $this->request->param('open_order_day/d',0);
        $name = $this->request->param('name');
        $send_scope = $this->request->param('send_scope');
        $order_send_time = $this->request->param('order_send_time');
        $start_send_price = $this->request->param('start_send_price','0.00');
        $base_freight = $this->request->param('base_freight','0.00');
        $order_time_slot = $this->request->param('order_time_slot',null);
        $ladder_freight_distance = $this->request->param('ladder_freight_distance');
        $reduce_freight_price= $this->request->param('reduce_freight_price');
        $forward_order_send = $this->request->param('forward_order_send');
        $remarks = $this->request->param('remarks');
        //var_dump($this->request->param());exit;
        if(!in_array($is_open,[0,1]) || !in_array($is_default,[0,1]) || !in_array($open_order_send,[0,1])) return $this->error('参数错误');

        $default_send_type = $this->defaultSendType();

        Db::startTrans();

        try{
            if(null === $city_setting=SendCityModel::where([['shop_id','=',$this->request->shopId]])->find()){

                $city_setting = new SendCityModel();
                $save_city_setting['shop_id'] = $this->request->shopId;

            }

            $save_city_setting['is_open'] = $is_open;
            $save_city_setting['is_default'] = $is_default;
            $save_city_setting['open_order_send'] = $open_order_send;
            $save_city_setting['open_order_day'] = $open_order_day;
            $save_city_setting['name'] = $name;

            $json_str=[];

            if($send_scope){
                //$send_scope = json_decode($send_scope,true);
                $json_str[]='send_scope';
            }

            if($order_send_time){
                //$order_send_time = json_decode($order_send_time,true);
                $json_str[]='order_send_time';
            }

            if($order_time_slot){
                //$order_time_slot = json_decode($order_time_slot,true);
                $json_str[]='order_time_slot';
            }

            if($forward_order_send){
                //$forward_order_send = json_decode($forward_order_send,true);
                $json_str[]='forward_order_send';
            }


            $save_city_setting['start_send_price'] = $start_send_price;

            $save_city_setting['base_freight'] = $base_freight;

            if($ladder_freight_distance){
                //$ladder_freight_distance =  json_decode($ladder_freight_distance,true);
                $json_str[]='ladder_freight_distance';
            }

            if($reduce_freight_price){
                //$reduce_freight_price =  json_decode($reduce_freight_price,true);
                $json_str[]='reduce_freight_price';
            }

            $save_city_setting['send_scope'] = $send_scope;
            $save_city_setting['order_send_time'] = $order_send_time;
            $save_city_setting['order_time_slot'] = $order_time_slot;
            $save_city_setting['forward_order_send'] = $forward_order_send;
            $save_city_setting['ladder_freight_distance'] = $ladder_freight_distance;
            $save_city_setting['reduce_freight_price'] = $reduce_freight_price;

            $save_city_setting['remarks'] = $remarks;

            $city_setting->where([['shop_id','=',$this->request->shopId]])->json($json_str,true)->update($save_city_setting);

            if($is_default>0){
                if($is_open<1) return $this->error('默认配送方式不能关闭');

                if($default_send_type!==3) $this->updateDefaultSendType($default_send_type,['is_default'=>0]);

                ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>3]);
            }else{
                if($city_setting->is_default>0) return $this->error('必须开启一种默认配送方式');
//                if($default_send_type == 3){
//                    ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>0]);
//                }
            }

            Db::commit();

            return $this->success('保存成功');

        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
    }

    /**
     * 获取自提信息
     */
    public function showSelf()

    {

        if(null === $self_setting=SendSelfModel::where([['shop_id','=',$this->request->shopId]])->json(['order_time_slot','order_self_time'],true)->find()){

            $self_setting = new SendSelfModel();
            $self_setting->shop_id = $this->request->shopId;
            $self_setting->save();

        }

        $detail=[
            'is_open' => $self_setting->is_open ?? 0,
            'name' => $self_setting->name,
            'is_default' => $self_setting->is_default ?? 0,
            'open_order_self' => $self_setting->open_order_self ?? 0,
            'open_order_day' => $self_setting->open_order_day ?? 0,
            'order_self_time' => $self_setting->order_self_time ??[],
            'order_time_slot' => $self_setting->order_time_slot ?? (object)[],
            'remarks' => $self_setting->remarks,
        ];

        return $this->success(['detail'=>$detail]);

    }

    /**
     * 保存自提信息
     */
    public function saveSelf()

    {
        $is_open = $this->request->param('is_open/d',0);
        $is_default = $this->request->param('is_default/d',0);
        $open_order_self = $this->request->param('open_order_self/d',0);
        $open_order_day = $this->request->param('open_order_day/d',0);
        $name = $this->request->param('name');
        $order_self_time = $this->request->param('order_self_time');
        $order_time_slot = $this->request->param('order_time_slot');
        $remarks = $this->request->param('remarks');

        if(!in_array($is_open,[0,1]) || !in_array($is_default,[0,1]) || !in_array($open_order_self,[0,1])) return $this->error('参数错误');

        $default_send_type = $this->defaultSendType();

        Db::startTrans();

        try{
            if(null === $self_setting=SendSelfModel::where([['shop_id','=',$this->request->shopId]])->find()){
                $self_setting = new SendSelfModel();
                $save_self_setting['shop_id'] = $this->request->shopId;
            }


            $save_self_setting['is_open'] = $is_open;
            $save_self_setting['is_default'] = $is_default;
            $save_self_setting['open_order_self'] = $open_order_self;
            $save_self_setting['open_order_day'] = $open_order_day;
            $save_self_setting['name'] = $name;

            $json_str=[];
            if($order_self_time){
                //$order_self_time = json_decode($order_self_time,true);
                $json_str[] = 'order_self_time';
            }
            if($order_time_slot){
                //$order_time_slot = json_decode($order_time_slot,true);
                $json_str[] = 'order_time_slot';
            }

            $save_self_setting['order_self_time'] = $order_self_time;
            $save_self_setting['order_time_slot'] = $order_time_slot;

            $save_self_setting['remarks'] = $remarks;

            $self_setting->where([['shop_id','=',$this->request->shopId]])->json($json_str)->update($save_self_setting);

            if($is_default>0){
                if($is_open<1) return $this->error('默认配送方式不能关闭');

                if($default_send_type!==2) $this->updateDefaultSendType($default_send_type,['is_default'=>0]);

                ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>2]);
            }else{
                if($self_setting->is_default>0) return $this->error('必须开启一种默认配送方式');
//                if($default_send_type == 2){
//                    ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>0]);
//                }
            }

            Db::commit();

            return $this->success('保存成功');

        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
    }
    public function updateDefaultSendType($default_send_type,$update){
//        if($default_send_type == 1){
//            SendKuaidiModel::where([['shop_id','=',$this->request->shopId]])->update($update);
//        }
        if($default_send_type == 1){
            FreightTemplateModel::where([['shop_id','=',$this->request->shopId]])->update($update);
        }
        if($default_send_type == 2){
            SendSelfModel::where([['shop_id','=',$this->request->shopId]])->update($update);
        }
        if($default_send_type == 3){
            SendCityModel::where([['shop_id','=',$this->request->shopId]])->update($update);
        }
        return true;

    }


    public function defaultSendType(){
        $default=0;

//        if(null !== $kuaidi_setting=SendKuaidiModel::where([['shop_id','=',$this->request->shopId],['is_default','=',1]])->find()){
//
//            $default=1;
//        }
        if(null !== $kuaidi_setting=FreightTemplateModel::where([['shop_id','=',$this->request->shopId],['is_default','=',1]])->find()){

            $default=1;
        }
        if(null !== $city_setting=SendCityModel::where([['shop_id','=',$this->request->shopId],['is_default','=',1]])->find()){

            $default=3;
        }
        if(null !== $self_setting=SendSelfModel::where([['shop_id','=',$this->request->shopId],['is_default','=',1]])->find()){

            $default=2;
        }

        return $default;

    }

}